Challenge 9

Author

Jun Noh

Loading in libraries/dataset

Code
library(tidyverse)
library(here)
library(broom)
library(knitr)
library(kableExtra)
library(DT)
Code
babyNames <- read_csv(here::here("Week9", "lab9", "StateNames_A.csv")) |> 
  rename(Sex = "Gender")

Interactive dataset for babyNames

Code
datatable(babyNames)

Summarizing & Visualizing the Number of Allisons

Code
babyNames |> 
  mutate(Count = replace_na(Count, 0)) |> 
  group_by(Sex, State) |> 
  filter(Name == "Allison", Sex == "F") |> 
  summarize(total = sum(Count)) |> 
  kable(caption = "<center><strong>Frequencies of babies named 
        Allison by state</strong></center>") |> 
  kable_styling(bootstrap_options = "striped", html_font = "Cambria") 
Frequencies of babies named Allison by state
Sex State total
F AK 232
F AL 1535
F AR 1198
F AZ 1880
F CA 12413
F CO 1594
F CT 1099
F DC 321
F DE 294
F FL 4455
F GA 3257
F HI 183
F IA 1477
F ID 451
F IL 5110
F IN 3067
F KS 1283
F KY 1905
F LA 1209
F MA 2218
F MD 2229
F ME 340
F MI 4014
F MN 2374
F MO 2882
F MS 817
F MT 226
F NC 3435
F ND 285
F NE 807
F NH 412
F NJ 3052
F NM 399
F NV 729
F NY 5747
F OH 5487
F OK 1421
F OR 1186
F PA 4307
F RI 306
F SC 1228
F SD 376
F TN 2488
F TX 10192
F UT 1125
F VA 3220
F VT 135
F WA 1956
F WI 2367
F WV 813
F WY 142

Alan dataset

Code
A_names <- babyNames |> 
  filter(Name %in% c("Alan", "Allen", "Allan"), Sex == "M")

Interactive dataset for A_names

Code
datatable(A_names)

Total Counts

Code
A_names |> 
  filter(State %in% c("CA", "PA"), Year == "2000") |> 
  pivot_wider(names_from = Name, values_from = Count) |> 
  mutate(across(Alan:Allan, .fns = replace_na, 0)) |> 
  rowwise() |> 
  mutate(total = sum(Alan, Allen, Allan)) |> 
  kable(caption = "<center><strong>Total counts of Different Alan 
        spellings in CA and PA in 2000</strong></center>") |> 
  kable_styling(bootstrap_options = "striped", html_font = "Cambria") 
Total counts of Different Alan spellings in CA and PA in 2000
Year Sex State Alan Allen Allan total
2000 M CA 579 176 131 886
2000 M PA 51 56 12 119

Percentages

Code
A_names |> 
  filter(State %in% c("CA", "PA"), Year == "2000") |> 
  pivot_wider(names_from = Name, values_from = Count) |> 
  mutate(across(Alan:Allan, .fns = replace_na, 0)) |> 
  rowwise() |> 
  mutate(total = sum(Alan, Allen, Allan), Alan = Alan/total * 100, 
         Allen = Allen/total * 100, Allan = Allan/total * 100, 
         totalPct = sum(Alan, Allen, Allan)) |> 
  kable(caption = "<count><strong>Percentages of different Alan 
        spellings in CA and PA in 2000 rounded 
        to 2 decimal places</strong></center>", 
        digits = 2) |> 
  kable_styling(bootstrap_options = "striped", html_font = "Cambria") 
Percentages of different Alan spellings in CA and PA in 2000 rounded to 2 decimal places
Year Sex State Alan Allen Allan total totalPct
2000 M CA 65.35 19.86 14.79 886 100
2000 M PA 42.86 47.06 10.08 119 100